home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / WWW / Perl_WWW_Utilities / total / guestbook / guestbook.pl < prev    next >
Encoding:
Perl Script  |  1996-01-01  |  9.8 KB  |  365 lines

  1. #!/usr/bin/perl
  2.  
  3. # Guestbook for the World Wide Web
  4. # Created by Matt Wright           Version 2.3.1
  5. # Created on: 4/21/95             Last Modified: 10/29/95
  6. # Consult the file README for more information and Installation Instructions.
  7.  
  8. #############################################################################
  9. # Set Variables
  10.  
  11. $guestbookurl = "http://your.host.com/~yourname/guestbook.html";
  12. $guestbookreal = "/home/yourname/public_html/guestbook.html";
  13. $guestlog = "/home/yourname/public_html/guestlog.html";
  14. $cgiurl = "http://your.host.com/cgi-bin/guestbook.pl";
  15. $date_command = "/usr/bin/date";
  16.  
  17. # Set Your Options:
  18. $mail = 0;              # 1 = Yes; 0 = No
  19. $uselog = 1;            # 1 = Yes; 0 = No
  20. $linkmail = 1;          # 1 = Yes; 0 = No
  21. $separator = 1;         # 1 = <hr>; 0 = <p>
  22. $redirection = 0;       # 1 = Yes; 0 = No
  23. $entry_order = 1;       # 1 = Newest entries added first;
  24.                         # 0 = Newest Entries added last.
  25. $remote_mail = 0;       # 1 = Yes; 0 = No
  26. $allow_html = 1;        # 1 = Yes; 0 = No
  27. $line_breaks = 0;    # 1 = Yes; 0 = No
  28.  
  29. # If you answered 1 to $mail or $remote_mail you will need to fill out 
  30. # these variables below:
  31. $mailprog = '/usr/lib/sendmail';
  32. $recipient = 'you@your.com';
  33.  
  34. # Done
  35. #############################################################################
  36.  
  37.  
  38. # Get the Date for Entry
  39. $date = `$date_command +"%A, %B %d, %Y at %T (%Z)"`; chop($date);
  40. $shortdate = `$date_command +"%D %T %Z"`; chop($shortdate);
  41.  
  42. # Get the input
  43. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  44.  
  45. # Split the name-value pairs
  46. @pairs = split(/&/, $buffer);
  47.  
  48. foreach $pair (@pairs) {
  49.    ($name, $value) = split(/=/, $pair);
  50.  
  51.    # Un-Webify plus signs and %-encoding
  52.    $value =~ tr/+/ /;
  53.    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  54.    $value =~ s/<!--(.|\n)*-->//g;
  55.  
  56.    if ($allow_html != 1) {
  57.       $value =~ s/<([^>]|\n)*>//g;
  58.    }
  59.  
  60.    $FORM{$name} = $value;
  61. }
  62.  
  63. # Print the Blank Response Subroutines
  64. &no_comments unless $FORM{'comments'};
  65. &no_name unless $FORM{'realname'};
  66.  
  67. # Begin the Editing of the Guestbook File
  68. open (FILE,"$guestbookreal") || die "Can't Open $guestbookreal: $!\n";
  69. @LINES=<FILE>;
  70. close(FILE);
  71. $SIZE=@LINES;
  72.  
  73. # Open Link File to Output
  74. open (GUEST,">$guestbookreal") || die "Can't Open $guestbookreal: $!\n";
  75.  
  76. for ($i=0;$i<=$SIZE;$i++) {
  77.    $_=$LINES[$i];
  78.    if (/<!--begin-->/) { 
  79.  
  80.       if ($entry_order eq '1') {
  81.          print GUEST "<!--begin-->\n";
  82.       }
  83.    
  84.       if ($line_breaks == 1) {
  85.          $FORM{'comments'} =~ s/\cM\n/<br>\n/g;
  86.       }
  87.  
  88.       print GUEST "<b>$FORM{'comments'}</b><br>\n";
  89.  
  90.       if ($FORM{'url'}) {
  91.          print GUEST "<a href=\"$FORM{'url'}\">$FORM{'realname'}</a>";
  92.       }
  93.       else {
  94.          print GUEST "$FORM{'realname'}";
  95.       }
  96.  
  97.       if ( $FORM{'username'} ){
  98.          if ($linkmail eq '1') {
  99.             print GUEST " \<<a href=\"mailto:$FORM{'username'}\">";
  100.             print GUEST "$FORM{'username'}</a>\>";
  101.          }
  102.          else {
  103.             print GUEST " <$FORM{'username'}>";
  104.          }
  105.       }
  106.  
  107.       print GUEST "<br>\n";
  108.  
  109.       if ( $FORM{'city'} ){
  110.          print GUEST "$FORM{'city'},";
  111.       }
  112.      
  113.       if ( $FORM{'state'} ){
  114.          print GUEST " $FORM{'state'}";
  115.       }
  116.  
  117.       if ( $FORM{'country'} ){
  118.          print GUEST " $FORM{'country'}";
  119.       }
  120.  
  121.       if ($separator eq '1') {
  122.          print GUEST " - $date<hr>\n\n";
  123.       }
  124.       else {
  125.          print GUEST " - $date<p>\n\n";
  126.       }
  127.  
  128.       if ($entry_order eq '0') {
  129.          print GUEST "<!--begin-->\n";
  130.       }
  131.  
  132.    }
  133.    else {
  134.       print GUEST $_;
  135.    }
  136. }
  137.  
  138. close (GUEST);
  139.  
  140. # Log The Entry
  141.  
  142. if ($uselog eq '1') {
  143.    &log('entry');
  144. }
  145.  
  146.  
  147. #########
  148. # Options
  149.  
  150. # Mail Option
  151. if ($mail eq '1') {
  152.    open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";
  153.  
  154.    print MAIL "Reply-to: $FORM{'username'} ($FORM{'realname'})\n";
  155.    print MAIL "From: $FORM{'username'} ($FORM{'realname'})\n";
  156.    print MAIL "Subject: Entry to Guestbook\n\n";
  157.    print MAIL "You have a new entry in your guestbook:\n\n";
  158.    print MAIL "------------------------------------------------------\n";
  159.    print MAIL "$FORM{'comments'}\n";
  160.    print MAIL "$FORM{'realname'}";
  161.  
  162.    if ( $FORM{'username'} ){
  163.       print MAIL " <$FORM{'username'}>";
  164.    }
  165.  
  166.    print MAIL "\n";
  167.  
  168.    if ( $FORM{'city'} ){
  169.       print MAIL "$FORM{'city'},";
  170.    }
  171.  
  172.    if ( $FORM{'state'} ){
  173.       print MAIL " $FORM{'state'}";
  174.    }
  175.  
  176.    if ( $FORM{'country'} ){
  177.       print MAIL " $FORM{'country'}";
  178.    }
  179.  
  180.    print MAIL " - $date\n";
  181.    print MAIL "------------------------------------------------------\n";
  182.  
  183.    close (MAIL);
  184. }
  185.  
  186. if ($remote_mail eq '1' && $FORM{'username'}) {
  187.    open (MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";
  188.  
  189.    print MAIL "To: $FORM{'username'}\n";
  190.    print MAIL "From: $recipient\n";
  191.    print MAIL "Subject: Entry to Guestbook\n\n";
  192.    print MAIL "Thank you for adding to my guestbook.\n\n";
  193.    print MAIL "------------------------------------------------------\n";
  194.    print MAIL "$FORM{'comments'}\n";
  195.    print MAIL "$FORM{'realname'}";
  196.  
  197.    if ( $FORM{'username'} ){
  198.       print MAIL " <$FORM{'username'}>";
  199.    }
  200.  
  201.    print MAIL "\n";
  202.  
  203.    if ( $FORM{'city'} ){
  204.       print MAIL "$FORM{'city'},";
  205.    }
  206.  
  207.    if ( $FORM{'state'} ){
  208.       print MAIL " $FORM{'state'}";
  209.    }
  210.  
  211.    if ( $FORM{'country'} ){
  212.      print MAIL " $FORM{'country'}";
  213.    }
  214.  
  215.    print MAIL " - $date\n";
  216.    print MAIL "------------------------------------------------------\n";
  217.  
  218.    close (MAIL);
  219. }
  220.  
  221. # Print Out Initial Output Location Heading
  222. if ($redirection eq '1') {
  223.    print "Location: $guestbookurl\n\n";
  224. }
  225. else { 
  226.    &no_redirection;
  227. }
  228.  
  229. #######################
  230. # Subroutines
  231.  
  232. sub no_comments {
  233.    print "Content-type: text/html\n\n";
  234.    print "<html><head><title>No Comments</title></head>\n";
  235.    print "<body><h1>Your Comments appear to be blank</h1>\n";
  236.    print "The comment section in the guestbook fillout form appears\n";
  237.    print "to be blank and therefore the Guestbook Addition was not\n";
  238.    print "added.  Please enter your comments below.<p>\n";
  239.    print "<form method=POST action=\"$cgiurl\">\n";
  240.    print "Your Name:<input type=text name=\"realname\" size=30 ";
  241.    print "value=\"$FORM{'realname'}\"><br>\n";
  242.    print "E-Mail: <input type=text name=\"username\""; 
  243.    print "value=\"$FORM{'username'}\" size=40><br>\n";
  244.    print "City: <input type=text name=\"city\" value=\"$FORM{'city'}\" ";
  245.    print "size=15>, State: <input type=text name=\"state\" "; 
  246.    print "value=\"$FORM{'state'}\" size=15> Country: <input type=text "; 
  247.    print "name=\"country\" value=\"$FORM{'country'}\" size=15><p>\n";
  248.    print "Comments:<br>\n";
  249.    print "<textarea name=\"comments\" COLS=60 ROWS=4></textarea><p>\n";
  250.    print "<input type=submit> * <input type=reset></form><hr>\n";
  251.    print "Return to the <a href=\"$guestbookurl\">Guestbook</a>.";
  252.    print "\n</body></html>\n";
  253.  
  254.    # Log The Error
  255.    if ($uselog eq '1') {
  256.       &log('no_comments');
  257.    }
  258.  
  259.    exit;
  260. }
  261.  
  262. sub no_name {
  263.    print "Content-type: text/html\n\n";
  264.    print "<html><head><title>No Name</title></head>\n";
  265.    print "<body><h1>Your Name appears to be blank</h1>\n";
  266.    print "The Name Section in the guestbook fillout form appears to\n";
  267.    print "be blank and therefore your entry to the guestbook was not\n";
  268.    print "added.  Please add your name in the blank below.<p>\n";
  269.    print "<form method=POST action=\"$cgiurl\">\n";
  270.    print "Your Name:<input type=text name=\"realname\" size=30><br>\n";
  271.    print "E-Mail: <input type=text name=\"username\"";
  272.    print " value=\"$FORM{'username'}\" size=40><br>\n";
  273.    print "City: <input type=text name=\"city\" value=\"$FORM{'city'}\" ";
  274.    print "size=15>, State: <input type=text name=\"state\" ";
  275.    print "value=\"$FORM{'state'}\" size=2> Country: <input type=text ";
  276.    print "value=USA name=\"country\" value=\"$FORM{'country'}\" ";
  277.    print "size=15><p>\n";
  278.    print "Comments have been retained.<p>\n";
  279.    print "<input type=hidden name=\"comments\" ";
  280.    print "value=\"$FORM{'comments'}\">\n";
  281.    print "<input type=submit> * <input type=reset><hr>\n";
  282.    print "Return to the <a href=\"$guestbookurl\">Guestbook</a>.";
  283.    print "\n</body></html>\n";
  284.  
  285.    # Log The Error
  286.    if ($uselog eq '1') {
  287.       &log('no_name');
  288.    }
  289.  
  290.    exit;
  291. }
  292.  
  293. # Log the Entry or Error
  294. sub log {
  295.    $log_type = $_[0];
  296.    open (LOG, ">>$guestlog");
  297.    if ($log_type eq 'entry') {
  298.       print LOG "$ENV{'REMOTE_HOST'} - [$shortdate]<br>\n";
  299.    }
  300.    elsif ($log_type eq 'no_name') {
  301.       print LOG "$ENV{'REMOTE_HOST'} - [$shortdate] - ERR: No Name<br>\n";
  302.    }
  303.    elsif ($log_type eq 'no_comments') {
  304.       print LOG "$ENV{'REMOTE_HOST'} - [$shortdate] - ERR: No ";
  305.       print LOG "Comments<br>\n";
  306.    }
  307. }
  308.  
  309. # Redirection Option
  310. sub no_redirection {
  311.  
  312.    # Print Beginning of HTML
  313.    print "Content-Type: text/html\n\n";
  314.    print "<html><head><title>Thank You</title></head>\n";
  315.    print "<body><h1>Thank You For Signing The Guestbook</h1>\n";
  316.  
  317.    # Print Response
  318.    print "Thank you for filling in the guestbook.  Your entry has\n";
  319.    print "been added to the guestbook.<hr>\n";
  320.    print "Here is what you submitted:<p>\n";
  321.    print "<b>$FORM{'comments'}</b><br>\n";
  322.  
  323.    if ($FORM{'url'}) {
  324.       print "<a href=\"$FORM{'url'}\">$FORM{'realname'}</a>";
  325.    }
  326.    else {
  327.       print "$FORM{'realname'}";
  328.    }
  329.  
  330.    if ( $FORM{'username'} ){
  331.       if ($linkmail eq '1') {
  332.          print " <<a href=\"mailto:$FORM{'username'}\">";
  333.          print "$FORM{'username'}</a>>";
  334.       }
  335.       else {
  336.          print " <$FORM{'username'}>";
  337.       }
  338.    }
  339.  
  340.    print "<br>\n";
  341.  
  342.    if ( $FORM{'city'} ){
  343.       print "$FORM{'city'},";
  344.    }
  345.  
  346.    if ( $FORM{'state'} ){
  347.       print " $FORM{'state'}";
  348.    }
  349.  
  350.    if ( $FORM{'country'} ){
  351.       print " $FORM{'country'}";
  352.    }
  353.  
  354.    print " - $date<p>\n";
  355.  
  356.    # Print End of HTML
  357.    print "<hr>\n";
  358.    print "<a href=\"$guestbookurl\">Back to the Guestbook</a>\n";         print "- You may need to reload it when you get there to see your\n";
  359.    print "entry.\n";
  360.    print "</body></html>\n";
  361.  
  362.    exit;
  363. }
  364.  
  365.